﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DevExpress.Web.ASPxEditors;
using CurriculumData;

namespace CurriculumSite
{
	public partial class SubscribeWebUserControl : UserControl
	{
        #region Members

        private readonly Controller _controller = new Controller();

        #endregion

        #region Events

        public event EventHandler Subscribed;

        #endregion

        #region Event Handlers

        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack)
                return;
            foreach (var area in _controller.Areas)
                areaASPxComboBox.Items.Add(area[0].ToString(), area[0]);
        }

        protected void areaASPxComboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            subAreaASPxComboBox.Items.Clear();
            subAreaASPxComboBox.Text = "";
            positionASPxComboBox.Items.Clear();
            positionASPxComboBox.Text = "";
            foreach (var subArea in _controller.GetSubAreas(areaASPxComboBox.Text))
                subAreaASPxComboBox.Items.Add(subArea[0].ToString(), subArea[0]);
        }

        protected void subAreaASPxComboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            positionASPxComboBox.Items.Clear();
            positionASPxComboBox.Text = "";
            foreach (var position in _controller.GetPositions(areaASPxComboBox.Text, subAreaASPxComboBox.Text))
                positionASPxComboBox.Items.Add(position[0].ToString(), position[0]);
        }

        protected void subscribeASPxButton_Click(object sender, EventArgs e)
        {
            if (positionASPxComboBox.Text == String.Empty)
                return;
            _controller.Subscribe((int)Session["CandidateId"], areaASPxComboBox.Text, subAreaASPxComboBox.Text, positionASPxComboBox.Text);
            OnSubscribed();
        }

        #endregion

        #region Private Methods

        private void OnSubscribed()
        {
            EventHandler subscribed = Subscribed;
            if (subscribed != null)
                subscribed(this, new EventArgs());
        }

        #endregion
	}
}   